/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShEquipmentCommLogDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShEquipmentCommLogService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShEquipmentCommLog;
import com.smarthome.domain.enums.EquipmentTypeEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShEquipmentCommLogService 实现类
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shEquipmentCommLogService")
public class ShEquipmentCommLogServiceImpl extends BaseServiceImpl<ShEquipmentCommLog, Long> implements ShEquipmentCommLogService {

    @Resource
    private ShEquipmentCommLogDao shEquipmentCommLogDao;

    public BaseDao<ShEquipmentCommLog, Long> getDao() {
        return shEquipmentCommLogDao;
    }

	@Override
	public DataGrid queryShEquipmentCommLogs(
			ShEquipmentCommLog equipmentCommLog, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
		ShEquipmentCommLog queryShEquipmentCommLog = equipmentCommLog;
        if (!StringUtils.isBlank(page.getSort())) {
        	queryShEquipmentCommLog.setOrderField(page.getSort());
        }
        if (!StringUtils.isBlank(page.getOrder())) {
        	queryShEquipmentCommLog.setOrderFieldType(page.getOrder());
        }
        /*
        if (!StringUtils.isBlank(equipmentCommLog.getEquipmentName())) {
        	queryShEquipmentCommLog.addQueryData("equipmentName", equipmentCommLog.getEquipmentName());
        }
        if (!StringUtils.isBlank(equipmentCommLog.getEquipmentType())) {
        	queryShEquipmentCommLog.addQueryData("equipmentType", equipmentCommLog.getEquipmentType());
        }*/
        
        
        
        long totalCount = shEquipmentCommLogDao.queryShEquipmentCommLogsCount(queryShEquipmentCommLog);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
        	queryShEquipmentCommLog.setStartIndex(startIndex);
        	queryShEquipmentCommLog.setEndIndex(endIndex);
            List<ShEquipmentCommLog> shEquipmentCommLogs = shEquipmentCommLogDao.queryShEquipmentCommLogs(queryShEquipmentCommLog);
            for(ShEquipmentCommLog logs:shEquipmentCommLogs) {
            	logs.setEquipmentTypeName(EquipmentTypeEnum.getNameByCode(logs.getEquipmentType()));
            }
            dataGrid.setRows(shEquipmentCommLogs);
        }
        return dataGrid;
	}

	@Override
	public int record(ShEquipmentCommLog equipmentCommLog) {
		return saveOrUpdate(equipmentCommLog);
	}

}